<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/19
 * Time: 15:14
 */
namespace app\admin\controller;
use think\File;
use think\Request;
use think\Db;

class EquityController extends CommonController
{
    /**
     * 获取海家权益信息
     */
    public function getData() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $key = input('key');
        $map = [];
        if($key != ""){
            $map['equity_title'] = ['like',"%" . $key . "%"];
        }
        $notice = Db::table('ew_equity')
            ->field('equity_id,equity_title,equity_introduce,equity_route,equity_integral,equity_type,equity_img')
            ->where($map)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_equity')
            ->where($map)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json($data);
    }

    /**
     * 添加权益信息
     * @return mixed
     */
    public function addEquity(){
        return $this->fetch('equity/addequity');
    }

    /**
     * 添加权益
     * @return \think\response\Json
     */
    public function add(){
        extract(input());
        $param = [];
        if($_FILES['file']['name'][0]!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['equity_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $time = date('Y-m-d H:i:s');
        $param['equity_title'] = $title;
        $param['equity_introduce'] = $introduce;
        $param['equity_route'] = $route;
        $param['equity_integral'] = $integral;
        $param['equity_type'] = $cate;
        $param['equity_create_time'] = $time;
        $param['equity_update_time'] = $time;
        $res = Db::table('ew_equity')->insert($param);
        if($res){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>100,'msg'=>'添加失败']);
        }
    }

    /**
     * 修改权益页面
     * @return mixed
     */
    public function updateEquity(){
        $res = Db::table('ew_equity')->where('equity_id',input('id'))->find();
        $this->assign ('res',$res);
        return $this->fetch('equity/updateequity');
    }

    /**
     * 修改权益
     */
    public function update(){
        extract(input());
        $param = [];
        if($_FILES['file']['name']!='') {
            $tmp = $_FILES['file']['tmp_name'];
            $names = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $info = $this->uploadFile($tmp,$names,$size);
            if($info['status'] == 200){
                $param['equity_img'] =$info['msg'];
            }else{
                return json(['code'=>100,'msg'=>$info['msg']]);
            }
        }
        $time = date('Y-m-d H:i:s');
        $param['equity_title'] = $title;
        $param['equity_introduce'] = $introduce;
        $param['equity_route'] = $route;
        $param['equity_integral'] = $integral;
        $param['equity_type'] = $cate;
        $param['equity_update_time'] = $time;
        $res = Db::table('ew_equity')->where('equity_id',$id)->update($param);
        if($res){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>100,'msg'=>'修改失败']);
        }
    }
    /**
     * 删除房产
     * @return \think\response\Json
     */
    public function delEquity(){
        $id = input('id');
        $res = Db::table('ew_equity')->delete($id);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}